package com.huitone.smspfm.interfacz.tasks;

import java.util.concurrent.TimeUnit;

import com.huitone.smspfm.interfacz.core.AppStaticConfigs;
import com.huitone.smspfm.interfacz.logger.ExtLogger;

/**   
* @Title ReloadConfigTimingTask.java 
* @Package com.huitone.smspfm.interfacz.tasks 
* @Description 定时加载配置文件
* @author ybliang   
* @date 2017年10月19日 下午4:19:39 
* @version V1.0   
*/
public class ReloadConfigTimingTask extends Thread {
	
	private static final ExtLogger logger = ExtLogger.create(ReloadConfigTimingTask.class);
	
	private static SmsPlatformSwitchTask smsPlatformSwitchTask = null;
	
	private boolean keepGoing = true;

	@Override
	public void run() {
		
		while(!this.isInterrupted() && isKeepGoing()) {
			
			logger.info("重载配置信息");
			//保留平台运行状态变量 
			boolean platformRuningStatus = AppStaticConfigs.smsRuningFlag;
			AppStaticConfigs.reload();
			//检查平台是否切换
			if(platformRuningStatus != AppStaticConfigs.smsRuningFlag){
				startSmsPlatformSwitchTask(); 
			}
			
			try {
				TimeUnit.MINUTES.sleep(AppStaticConfigs.reloadConfigInterval);
			} catch (InterruptedException e) {}
		}
		
		logger.info("定时重载配置信息任务退出!");
	}

	public boolean isKeepGoing() {
		return keepGoing;
	}

	public void setKeepGoing(boolean keepGoing) {
		this.keepGoing = keepGoing;
	}

	@Override
	public void destroy() {
		this.setKeepGoing(false);
		this.interrupt();
	}
	
	/**
	 * 启动平台切换数据处理线程 
	 */
	private static void startSmsPlatformSwitchTask() {
		if(smsPlatformSwitchTask == null || !smsPlatformSwitchTask.isAlive()){
			smsPlatformSwitchTask = new SmsPlatformSwitchTask();
			smsPlatformSwitchTask.start();
		}
	}

}
